package com.hb.trade.server.project.file.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.enums.PayTypeEnum;
import com.hb.core.pojo.project.file.dto.BidBuyRecordDTO;
import com.hb.core.pojo.project.file.entity.BidBuyRecord;
import com.hb.core.pojo.project.file.mapper.BidBuyRecordMapper;
import com.hb.trade.server.project.file.service.BidBuyRecordService;
import org.apache.commons.lang3.ObjectUtils;
import org.fzzn.core.model.enums.LogicEnum;
import org.fzzn.core.util.Enums;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 供应商购标记录表 服务实现类
 * </p>
 *
 * @author zhaojk
 * @since 2021-09-16
 */
@Service
public class BidBuyRecordServiceImpl extends ServiceImpl<BidBuyRecordMapper, BidBuyRecord> implements BidBuyRecordService {

    @Override
    public Page<BidBuyRecordDTO> saleRecord(Page page, Long sectionId) {
        Page<BidBuyRecordDTO> records = baseMapper.saleRecord(page, sectionId);
        for (BidBuyRecordDTO bidBuyRecordDTO : records.getRecords()) {
            bidBuyRecordDTO.setDownloadFlagName(Enums.nameOf(LogicEnum.class, bidBuyRecordDTO.getDownloadFlag()));
            bidBuyRecordDTO.setPayStatusName(Enums.nameOf(LogicEnum.class, bidBuyRecordDTO.getOrderStatus()));
            bidBuyRecordDTO.setPayTypeName(Enums.nameOf(PayTypeEnum.class, bidBuyRecordDTO.getPayType()));
        }
        return records;
    }

    @Override
    public JSONObject sectionSaleInfo(Long sectionId) {
        if (null == sectionId) {
            return JSONUtil.createObj();
        }
        return baseMapper.sectionSaleInfo(sectionId);
    }

    @Override
    public BidBuyRecord findBidBuyRecordByOrderNo(String orderNo) {
        QueryWrapper<BidBuyRecord> bidBuyRecordQueryWrapper = new QueryWrapper<>();
        bidBuyRecordQueryWrapper.lambda().eq(BidBuyRecord::getOrderNo,orderNo).last("limit 1");
        return this.getOne(bidBuyRecordQueryWrapper);
    }

    /**
     *
     * @Description: 需要报名费的文件判断是否购买成功
     * @Author: zhongdl
     * @Date:2022/1/4 13:51
     * @param sectionId:  标段id
     * @param buyUserId: 购标人id
     * @param orgId: 企业id
     * @return: {@link Boolean }
     **/
    @Override
    public Boolean payFlag(Long sectionId, Long buyUserId, Long orgId) {
        QueryWrapper<BidBuyRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(BidBuyRecord::getSectionId,sectionId)
                .eq(BidBuyRecord::getBuyUserId,buyUserId)
                .eq(BidBuyRecord::getOrgId,orgId);
        return count(queryWrapper) > 0;
    }
    /**
     *
     * @Description: 查询当前企业和账户是否购买了标书
     * @Author: zhongdl
     * @Date:2022/1/28 09:31
     * @param sectionId:  标段id
     * @param buyUserId: 购标人id
     * @param orgId: 企业id
     * @Update： fengbin 修改根据购买人id查询条件 （若buyUserId为null 则查询当前企业购标信息）
     * @return: {@link Boolean }
     **/
    @Override
    public BidBuyRecord getBidBuyRecordByOrgId(Long sectionId, Long buyUserId, Long orgId) {
        QueryWrapper<BidBuyRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BidBuyRecord::getSectionId, sectionId);
        if(ObjectUtils.isNotEmpty(buyUserId)){
            queryWrapper.lambda().eq(BidBuyRecord::getBuyUserId, buyUserId);
        }
        queryWrapper.lambda().eq(BidBuyRecord::getOrgId, orgId);
        queryWrapper.lambda().last("limit 1");
        return getOne(queryWrapper);
    }
}
